package com.chengyu.core.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chengyu.core.constants.KeyConstant;
import com.chengyu.core.entity.TblPointsScene;
import com.chengyu.core.mapper.TblPointsSceneMapper;
import com.chengyu.core.response.TblPointsSceneResp;
import com.chengyu.core.service.ITblPointsSceneService;
import com.chengyu.core.util.ConvertUtils;
import com.chengyu.core.util.NewRedisUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 减排场景 服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2023-05-12
 */
@Service
@RequiredArgsConstructor
public class TblPointsSceneServiceImpl extends ServiceImpl<TblPointsSceneMapper, TblPointsScene> implements ITblPointsSceneService {

    private final NewRedisUtils newRedisUtils;

    @Override
    public List<TblPointsSceneResp> sceneList() {
        List<TblPointsSceneResp> list = new ArrayList<>();
        String key = KeyConstant.CARBON_EDUCTION_SCE_APPLET_LIST;
        Object object = newRedisUtils.get(key);
        if (object == null){
            List<TblPointsScene> sceneList = list(new LambdaQueryWrapper<TblPointsScene>()
//                    .eq(TblPointsScene::getChannel,channel)
                    .eq(TblPointsScene::getDelFlag,0)
                    .eq(TblPointsScene::getSceneStatus,0)
                    .orderByDesc(TblPointsScene::getSort));
            list = ConvertUtils.sourceToTarget(sceneList, TblPointsSceneResp.class);
            if (!CollUtil.isEmpty(list)){
                newRedisUtils.set(key, JSONObject.toJSONString(list));
            }
        }else {
            list = JSONObject.parseArray(object.toString(),TblPointsSceneResp.class);
        }
        return list;
    }

}
